package com.tianzj.mongodb.serivice.impl;

import com.mongodb.client.result.UpdateResult;
import com.tianzj.mongodb.dao.UserDao;
import com.tianzj.mongodb.entity.User;
import com.tianzj.mongodb.serivice.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: admin
 * @Date: 2019/7/22 15:29
 * @Description:
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private UserDao userDao;
    @Override
    public User findByName(String name) {
        return userDao.findByname(name);
    }

    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }

    @Override
    public User save(User user) {
        return userDao.insert(user);
    }

    @Override
    public UpdateResult update() {
        Update update = new Update();
        update.set("name","张三");
        Query query =new Query(Criteria.where("id").is("122"));
        UpdateResult updateResult = mongoTemplate.updateFirst(query, update, User.class);
        return updateResult;
    }
}
